package com.example.papercutting.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

public class FoldedShapeView extends View {
    private int shapeType; // 0 for circle, 1 for octagon, 2 for square
    private int foldCount = 8;
    private int color = Color.WHITE;

    public static final int SHAPE_CIRCLE = 0;
    public static final int SHAPE_OCTAGON = 1;
    public static final int SHAPE_SQUARE = 2;

    public FoldedShapeView(Context context) {
        super(context);
        init();
    }

    public FoldedShapeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public FoldedShapeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setLayerType(LAYER_TYPE_SOFTWARE, null); // Disable hardware acceleration
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(color);
        paint.setStyle(Paint.Style.FILL);

        float centerX = getWidth() / 2f;
        float centerY = getHeight() / 2f;
        float radius = Math.min(centerX, centerY);

        switch (shapeType) {
            case SHAPE_CIRCLE:
                drawCircleSector(canvas, paint, centerX, centerY, radius);
                break;
            case SHAPE_OCTAGON:
                drawOctagonSegment(canvas, paint, centerX, centerY, radius);
                break;
            case SHAPE_SQUARE:
                drawSquareTriangle(canvas, paint, centerX, centerY, radius);
                break;
        }
    }

    private void drawCircleSector(Canvas canvas, Paint paint, float centerX, float centerY, float radius) {
        RectF rectF = new RectF(centerX - radius, centerY - radius, centerX + radius, centerY + radius);
        float sweepAngle = 360f / foldCount;
        canvas.drawArc(rectF, 0, sweepAngle, true, paint);
    }

    private void drawOctagonSegment(Canvas canvas, Paint paint, float centerX, float centerY, float radius) {
        float innerRadius = radius / (float) Math.sqrt(2 - Math.sqrt(2));
        float outerRadius = radius;
        float angleStepInner = 360f / (foldCount * 2);
        float angleStepOuter = 360f / foldCount;

        Path path = new Path();
        path.moveTo(centerX, centerY);
        path.lineTo(
                centerX + innerRadius * (float) Math.cos(Math.toRadians(angleStepInner * 0)),
                centerY + innerRadius * (float) Math.sin(Math.toRadians(angleStepInner * 0))
        );
        path.lineTo(
                centerX + outerRadius * (float) Math.cos(Math.toRadians(angleStepOuter * 0)),
                centerY + outerRadius * (float) Math.sin(Math.toRadians(angleStepOuter * 0))
        );
        path.lineTo(
                centerX + innerRadius * (float) Math.cos(Math.toRadians(angleStepInner * 2)),
                centerY + innerRadius * (float) Math.sin(Math.toRadians(angleStepInner * 2))
        );
        path.close();

        canvas.drawPath(path, paint);
    }

    private void drawSquareTriangle(Canvas canvas, Paint paint, float centerX, float centerY, float radius) {
        float sideLength = radius * 2;
        float halfSide = sideLength / 2;
        float angleStep = 360f / foldCount;

        Path path = new Path();
        path.moveTo(centerX, centerY);
        path.lineTo(
                centerX + halfSide * (float) Math.cos(Math.toRadians(angleStep * 0)) - halfSide,
                centerY + halfSide * (float) Math.sin(Math.toRadians(angleStep * 0)) - halfSide
        );
        path.lineTo(
                centerX + halfSide * (float) Math.cos(Math.toRadians(angleStep * 1)) - halfSide,
                centerY + halfSide * (float) Math.sin(Math.toRadians(angleStep * 1)) - halfSide
        );
        path.close();

        canvas.drawPath(path, paint);
    }

    public void setShapeType(int shapeType) {
        this.shapeType = shapeType;
        invalidate(); // Redraw the view
    }

    public void setFoldCount(int foldCount) {
        this.foldCount = foldCount;
        invalidate(); // Redraw the view
    }

    public void setColor(int color) {
        this.color = color;
        invalidate(); // Redraw the view
    }
}



